package com.zy.openchat.core.manager;

import java.util.Objects;
import java.util.concurrent.PriorityBlockingQueue;

public class DownloadTask implements ITask{

    public String downloadTg;

    public long fileId;

    public long offset;

    private PriorityBlockingQueue<Integer> blockQueue;

    public DownloadTask(String downloadTg, long fileId, long offset) {
        this.downloadTg = downloadTg;
        this.fileId = fileId;
        this.offset = offset;
        blockQueue = new PriorityBlockingQueue<>();
    }


    @Override
    public long taskId() {
        return fileId;
    }

    @Override
    public long getDuration() {
        return 3*1000;
    }
    private int mSequence;
    @Override
    public void doTask() {
        TelegramManager.getTelegramManager().downloadFile(downloadTg,fileId,offset, TelegramManager.FileType.FileType_Message_Video);
        unLockBlock();
    }

    @Override
    public void setSequence(int mSequence) {
        this.mSequence = mSequence;
    }

    public void blockTask() throws Exception {
        blockQueue.take(); //如果队列里面没数据，就会一直阻塞
    }

    @Override
    public void unLockBlock() {
        blockQueue.add(1);
    }


    @Override
    public int getSequence() {
        return mSequence;
    }



    @Override
    public int compareTo(ITask another) {
        return mSequence==another.getSequence()?0:mSequence>another.getSequence()?1:-1;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DownloadTask that = (DownloadTask) o;
        return fileId == that.fileId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(fileId);
    }
}
